#!bin/bash

# ${#变量名}                     变量长度
# `expr length "$变量名"`        变量长度
# `command`和$(command)          命令替换(expr推荐用于比较, $()推荐用于四则运算)
# ${变量名#匹配规则}              变量开头进行匹配，将符合最短的数据删除
# ${变量名##匹配规则}             变量开头进行匹配，将符合最长的数据删除
# ${变量名%匹配规则}              变量尾部进行匹配，将符合最长的数据删除
# ${变量名%%匹配规则}             变量尾部进行匹配，将符合最长的数据删除
# ${变量名/旧字符串/新字符串}      变量内容符合旧字符串规则，将第一个旧字符串用新字符串替换
# ${变量名//旧字符串/新字符串}     变量内容符合旧字符串规则，将全部的旧字符串用新字符串替换

# declare/typeset -r variable=""  变量只读
# declare/typeset -i variable     整型变量
# declare/typeset -a variable     数组
string="Bigdata process framework is Hadoop,Hadoop is an open source project"
function print_tips
{
  echo "**************"
  echo "(1)打印string长度"
  echo "(2)删除字符串中的所有Hadoop"
  echo "(3)替换第一个Hadoop为Mapreduce"
  echo "(4)替换所有Hadoop为Mapreduce"
  echo "**************"
}

function len_of_string(){
  echo "`expr length "$string"`"
}

function del_hadoop(){
  echo "${string//Hadoop/}"
}

function rep_hadoop_mapreduce_first(){
  echo "${string/Hadoop/Mapreduce}"
}

function rep_hadoop_mapreduce_all(){
  echo "${string//Hadoop/Mapreduce}"
}

while true
do
  echo "[string=$string]"
  echo
  print_tips
  read -p "Pls input you choice(1|2|3|4|q|Q):" choice

  case $choice in 
    1)
      len_of_string
      ;;
    2)
      del_hadoop
      ;;
    3)
      rep_hadoop_mapreduce_first
      ;;
    4)
      rep_hadoop_mapreduce_all
      ;;
    q|Q)
      exit
      ;;
    *)
      echo "Error, input only in (1|2|3|4|q|Q)"
      ;;
  esac
done